package com.wookii.friedeggs.ui.gallery

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.asLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.bw.base.tool.BindingViewHolder
import com.bw.base.tool.newBindingViewHolder
import com.wookii.friedeggs.MainViewModel
import com.wookii.friedeggs.MainViewModelFactory
import com.wookii.friedeggs.MyApplication
import com.wookii.friedeggs.data.Exam
import com.wookii.friedeggs.databinding.FragmentGalleryBinding
import com.wookii.friedeggs.databinding.ItemExamBinding
import com.wookii.friedeggs.utils.getFullDate

class GalleryFragment : Fragment() {

    private var _binding: FragmentGalleryBinding? = null
    //通过ViewModelFactory初始化ViewModel
    private val viewModel: MainViewModel by activityViewModels {
        val studentDao = (activity?.application as MyApplication).database.studentDao()
        val examDao = (activity?.application as MyApplication).database.examDao()
        val classDao = (activity?.application as MyApplication).database.classDao()
        MainViewModelFactory(studentDao, examDao, classDao)
    }
    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val galleryViewModel =
            ViewModelProvider(this).get(GalleryViewModel::class.java)

        _binding = FragmentGalleryBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val textView: TextView = binding.textGallery
        galleryViewModel.text.observe(viewLifecycleOwner) {
            textView.text = it
        }
        val adapter = MyAdapter(mutableListOf()) { exam: Exam ->

        }
        binding.list.apply {
             this.adapter = adapter
            layoutManager = LinearLayoutManager(requireContext())

        }
        viewModel.getAllExam().observe(viewLifecycleOwner) {
            adapter.data = it
            adapter.notifyDataSetChanged()
            binding.textGallery.visibility = View.GONE
        }
        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    class MyAdapter(var data: MutableList<Exam>, function: (Exam) -> Unit) : Adapter<BindingViewHolder<ItemExamBinding>>() {
        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BindingViewHolder<ItemExamBinding> {
            return newBindingViewHolder(parent)
        }

        override fun getItemCount() = data.size

        override fun onBindViewHolder(holder: BindingViewHolder<ItemExamBinding>, position: Int) {
            data[position].apply {
                holder.binding.createDate.text = create.getFullDate()
            }
        }
    }
}